﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PumpCentralInterfaces;
using System.ServiceModel;
using PumeCentralService;
using PumpCentral_Domain.AlarmSubSystem;
using PumpCentral_Domain.MonitoringSubSystem;

namespace PumpCentral_Domain
{
    public delegate void PumpCommunicationEventHandler(Pump pump);
    public delegate void PumpPolyCommunicationEventHandler(List<PumpStatusDTO> dtolist);

    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] // jeg benytter denne metode fordi [OperationContract(IsOneWay)] ikke tillader at returnerer komplekse typer
    public class PumpServiceGateway : IPumpServiceGateway, IPumpCentralCallback
    {
        public event PumpCommunicationEventHandler PumpRegisterationRecieved;
        
        private PumpCentralSystem owner;
        private IPumpCentalService serviceConnection;
        public PumpServiceGateway(PumpCentralSystem owner)
        {
            this.owner = owner;
        }


        public void SendData()
        {
            
        }

        public List<Pump> getAllPumps()
        {
            List<Pump> pumpList = new List<Pump>();            

            try
            {
                DuplexChannelFactory<IPumpCentalService> channelFactory = new DuplexChannelFactory<IPumpCentalService>(new InstanceContext(this),new NetTcpBinding(),PumpCentralServiceKonstants.URI);
                serviceConnection = channelFactory.CreateChannel();

                List<PumpStatusDTO> dtoList = serviceConnection.GetDataFromAllPumps();

                foreach(PumpStatusDTO dto in dtoList)
                {
                    pumpList.Add(new Pump(){ID = dto.ID, WaterLevel = dto.waterLevel, Status =(PumpCentral_Domain.Status)dto.status});
                }

                return pumpList;
            }
            catch(CommunicationException ce)
            {
                return null;
            }


        }

        public void RaiseAlarm()
        {
      
        }        

        public bool StartPump()
        {
            return false;
        }

        public bool StopPump()
        {
            return false;
        }

        public double getWaterLevel()
        {
            throw new NotImplementedException();
        }

        public bool RegisterPump(PumpStatusDTO dto)
        {
            throw new NotImplementedException();
        }

        public bool UnregisterPump(PumpStatusDTO dto)
        {
            throw new NotImplementedException();
        }

        public PumpStatusDTO GetData()
        {
            throw new NotImplementedException();
        }
    }
}
